package com.activiti.z_six.service.impl;

import com.activiti.z_six.entity.taskAssignee.AssigneeUserEntity;
import com.activiti.z_six.entity.taskAssignee.FlowElementAttrs;
import com.activiti.z_six.entity.taskAssignee.OvProcessInstance;
import com.activiti.z_six.mapper.taskAssigneeMapper.AssigneeUserMapper;
import com.activiti.z_six.mapper.taskAssigneeMapper.FlowElementAttrsMapper;
import com.activiti.z_six.mapper.taskAssigneeMapper.OvProcessInstanceMapper;
import com.activiti.z_six.security.RedisUtils;
import com.activiti.z_six.service.IAssigneeUserService;
import com.activiti.z_six.service.manager.ProcessDefManager;
import com.activiti.z_six.util.SystemConfig;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.activiti.bpmn.model.*;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IAssigneeUserServiceImpl implements IAssigneeUserService {
    @Autowired
    private AssigneeUserMapper assigneeUserMapper;
    @Autowired
    private OvProcessInstanceMapper ovProcessInstanceMapper;
    @Autowired
    private FlowElementAttrsMapper flowElementAttrsMapper;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessDefManager processDefManager;
    @Autowired
    private RedisUtils redisUtils;
    private Long expireTime=Long.parseLong("360000") ;
    @Override
    public List<AssigneeUserEntity> getAssigneeUserWithTaskKey(String usertaskid,String ruleName){
        List<AssigneeUserEntity> assigneeUserEntityList;
        String assigneeString ="";
        if(redisUtils.exists(usertaskid+"_"+ruleName)){
            assigneeString=redisUtils.get(usertaskid+"_"+ruleName).toString();
        }

        //如果redis中不存在
        if (SystemConfig.IsNullOrEmpty(assigneeString)) {
            assigneeUserEntityList=assigneeUserMapper.getAssigneeUserWithTaskKey(usertaskid, ruleName);
        }
        else {
            assigneeUserEntityList=JSONObject.parseArray(assigneeString, AssigneeUserEntity.class);
            if(ruleName.equals("byUser")||ruleName.equals("byDept")) {
                if (SystemConfig.IsNullOrEmpty(assigneeUserEntityList.get(0).getUsername())
                        && SystemConfig.IsNullOrEmpty(assigneeUserEntityList.get(0).getName())) {
                    assigneeUserEntityList = assigneeUserMapper.getAssigneeUserWithTaskKey(usertaskid, ruleName);
                }
            }
        }
        return assigneeUserEntityList;
    }
    @Override
    public FlowElementAttrs getFormElement(String process_key){
        //获取流程模版信息
        OvProcessInstance ovProcessInstance=ovProcessInstanceMapper.getProcessInsLastVersion(process_key);
        BpmnModel bpmnModel = repositoryService.getBpmnModel(ovProcessInstance.getId_());
        //获取流程信息
        Process process=bpmnModel.getProcesses().get(0);
        StartEvent startEvent=(StartEvent)process.getInitialFlowElement();
        List<SequenceFlow> outgoingFlows = startEvent.getOutgoingFlows();
        //获取UserTask类型数据
        UserTask userTask = (UserTask) outgoingFlows.get(0).getTargetFlowElement();
        FlowElementAttrs flowElementAttrs=flowElementAttrsMapper.getFlowElementAttrs(userTask.getId());
        return flowElementAttrs;
    }
    @Override
    public String setAssigneeUser(JSONObject param){
        //人员数据
        JSONArray userlist = param.getJSONArray("userlist");
        //规则节点id标识
        String usertaskid=param.getString("usertaskid");
        //节点id
        String id=param.getString("id");
        //规则id
        String ruleName=param.getString("ruleName");
        //如果redis中已经存在，先删除
        if(redisUtils.exists(usertaskid)){
            redisUtils.remove(usertaskid);
        }
        //写入redis
        redisUtils.set(usertaskid,userlist.toString(),expireTime);

        List<AssigneeUserEntity> assigneeUserEntityList=assigneeUserMapper.getAssigneeUserWithTaskKey(id,ruleName);
        if(assigneeUserEntityList.size()>0){
            processDefManager.setAssigneeUser(userlist.toString(), id,ruleName);
        }
        return "设置成功";
    }
    @Override
    public String deleteAssigneeUser(String usertaskid, String username){
        AssigneeUserEntity assigneeUserEntity=new AssigneeUserEntity();
        assigneeUserEntity.setUsertaskid(usertaskid);
        assigneeUserEntity.setUsername(username);
        assigneeUserMapper.deleteAssigneeUser(assigneeUserEntity);

        return "执行成功";
    }
    @Override
    public String deleteAssigneeUserByTaskid(String usertaskid){
        assigneeUserMapper.deleteAssigneeUserByTaskid(usertaskid);
        return "执行成功";
    }
}
